package com.feishi.project.one;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2018-10-24
 * Time: 下午3:22
 */
public class PIDHeat  {
    float lastError;
    float preError;

    final float P_const = 40f;
    final float I_const = 150f;
    final float D_const = 1f;
    private float target;
    private float current;

    public PIDHeat() {
        preError = lastError = 0;
    }

    private float range(float heatTemp) {
        if (heatTemp < 0) {
            return 0;
        }
        if (heatTemp > 40) {
            return 40;
        }
        return heatTemp;
    }

//    @Override
    public float heat() {
        float currentError = target - current;

        System.out.println("PID params " + currentError + " " + lastError + " " + preError);

        float heatTemp = P_const * (currentError - lastError)
                + I_const * currentError
                + D_const * (currentError + preError - 2 * lastError);
        preError = lastError;
        lastError = currentError;
        heatTemp = range(heatTemp);
        System.out.println("PID out " + heatTemp);
        return heatTemp;
    }
}